<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="课程名称：">
            <el-input
              v-model="listQuery.courseName"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="来源机构：">
            <el-input
              v-model="listQuery.deptName"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
            <!--<el-select-->
            <!--v-model="listQuery.deptId"-->
            <!--filterable-->
            <!--remote-->
            <!--reserve-keyword-->
            <!--clearable-->
            <!--placeholder="请输入关键词"-->
            <!--:remote-method="remoteMethod"-->
            <!--:loading="loading">-->
            <!--<el-option-->
            <!--v-for="item in deptOptions"-->
            <!--:key="item.value"-->
            <!--:label="item.label"-->
            <!--:value="item.value">-->
            <!--</el-option>-->
            <!--</el-select>-->
          </el-form-item>
          <el-form-item label="类型：">
            <el-cascader
              v-model="listQuery.courseType"
              :options="courseTypeOptions"
              clearable
              :props="{
                children:'subset',
                label: 'typeName',
                value: 'id'
              }"
            >
            </el-cascader>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
      >新增课程</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          label="ID"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          label="课程名称"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.courseName }}</template>
        </el-table-column>
        <el-table-column
          label="类型"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.typeName }}</template>
        </el-table-column>
        <el-table-column
          label="来源机构"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.deptName }}</template>
        </el-table-column>
        <el-table-column
          label="最近更新"
          width="160"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.updateTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column
          label="更新频率"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.frequency }}</template>
        </el-table-column>
        <el-table-column
          label="视频数量"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.videoCount }}</template>
        </el-table-column>
        <el-table-column
          label="是否推荐"
          width="140"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              active-value="1"
              inactive-value="0"
              v-model="scope.row.isRecommend"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="240"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleVideoList(scope.$index, scope.row)"
            >课程视频
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleReview(scope.$index, scope.row)"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  curCourseBaseList,
  curCourseTypeList,
  curCourseBaseDelById,
  umsDeptList
} from '@/api/course'
import { deleteRecommend, saveRecommend } from '@/api/content'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  isRecommend: ''
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null,
      courseTypeOptions: [],
      loading: false,
      deptOptions: []
    }
  },
  created () {
    this.getCourseType()
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleStatusChange (index, row) {
      if (row.isRecommend === '1') {
        const list = { ...row }
        // creatTime 不用传
        // name 就是title
        // sourceId 就是id
        // releaseTime 就是createtime
        // sourceType 是页面中带的
        // channel 也是页面中带的
        const data = {
          channel: '1',
          releaseTime: list.createTime,
          name: list.courseName,
          image: list.coursePic,
          sourceId: list.id,
          sourceType: '01'
        }
        saveRecommend(data).then((response) => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
      }
      if (row.isRecommend === '0') {
        const params = {
          sourceId: row.id,
          sourceType: '01'
        }
        // console.log(row);
        deleteRecommend(params).then((response) => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
      }
    },
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteCouerse(row.id)
    },
    handleVideoList (index, row) {
      this.$router.push({
        path: '/course-ms/videoList',
        query: { id: row.id }
      })
    },
    handleReview (index, row) {
      this.$router.push({
        path: '/course-ms/reviewCourse',
        query: { id: row.id }
      })
    },
    handleAdd () {
      this.$router.push({ path: '/course-ms/addCourse' })
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/course-ms/updateCourse',
        query: { id: row.id }
      })
    },
    getList () {
      this.listLoading = true
      const { courseType, deptId } = this.listQuery
      const params = {
        ...this.listQuery
      }
      if (courseType && courseType.length > 0) {
        params.courseType = courseType[courseType.length - 1]
      }
      if (deptId) {
        params.deptName = deptId
      }
      curCourseBaseList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    deleteCouerse (ids) {
      this.$confirm('是否要删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('id', ids)
        curCourseBaseDelById(params).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    getCourseType () {
      this.courseTypeOptions = []
      // 获取课程类型枚举
      curCourseTypeList().then((response) => {
        this.courseTypeOptions = response.data.list
      })
    },
    remoteMethod (query) {
      if (query !== '') {
        this.loading = true
        umsDeptList({ pageSize: 50, pageNum: 1 }).then((response) => {
          this.loading = false
          this.deptOptions = response.data.list.map((item) => {
            return { value: `${item.id}`, label: `${item.deptName}` }
          })
        })
      } else {
        this.deptOptions = []
      }
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
